package controllers.image;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import controllers.image.upyunutil.CompressHandler;
import controllers.image.upyunutil.Result;
import play.Play;

/**
 * UpYun工厂类，负责创建UpYun对象。
 * @author burgess yang.
 *
 */
public class UpYunFactory {
	
	private static UpYun instance = null;
	private static UpYun instance1 = null;
	
	private static String bucketname = "industrial"; 
	private static String bucketname1 = "industrial";
	private static String username ="dailei"; 
	private static String password ="dailei123"; 
	private static String defaultBackName0 = "http://industrial.b0.upaiyun.com";
	private static String defaultBackName1 = "bkc.file.upyun.bucketname";
	
	
	public static UpYun createUpYun(){
		System.out.println();
		if(instance==null){
			//从配置文件读取又拍云空间配置信息。
			if(Play.configuration!=null){
				bucketname=Play.configuration.getProperty(defaultBackName0, bucketname);
				username=Play.configuration.getProperty("bkc.upyun.username", "bangkanche01");
				password=Play.configuration.getProperty("bkc.upyun.password", "bangkanche001");
			}
			instance = new UpYun(bucketname, username, password);
		}else{
			if(!instance.bucketName.equals(bucketname)){
				//从配置文件读取又拍云空间配置信息。
				if(Play.configuration!=null){
					bucketname=Play.configuration.getProperty(defaultBackName0, bucketname);
					username=Play.configuration.getProperty("bkc.upyun.username", "bangkanche01");
					password=Play.configuration.getProperty("bkc.upyun.password", "bangkanche001");
				}
				instance = new UpYun(bucketname, username, password);
			}
		}
		return instance;
	}
	
	public static UpYun createUpYunForFile(){
		if(instance1==null){
			//从配置文件读取又拍云空间配置信息。
			if(Play.configuration!=null){
				bucketname1=Play.configuration.getProperty(defaultBackName1, bucketname1);
				username=Play.configuration.getProperty("bkc.upyun.username", "bangkanche01");
				password=Play.configuration.getProperty("bkc.upyun.password", "bangkanche001");
			}
			instance1 = new UpYun(bucketname1, username, password);
		}else{
			if(!instance1.bucketName.equals(bucketname1)){
				//从配置文件读取又拍云空间配置信息。
				if(Play.configuration!=null){
					bucketname1=Play.configuration.getProperty(defaultBackName1, bucketname1);
					username=Play.configuration.getProperty("bkc.upyun.username", "bangkanche01");
					password=Play.configuration.getProperty("bkc.upyun.password", "bangkanche001");
				}
				instance1 = new UpYun(bucketname1, username, password);
			}
		}
		return instance1;
	}
	
	public static String createUpYunForZip(String [] imgs,String fileName,String type) throws JSONException{
		  CompressHandler handler = new CompressHandler(bucketname1, username, password);

	        //初始化参数组 Map
	        Map<String, Object> paramsMap = new HashMap<String, Object>();

	        //空间名
	        if("1".equals(type)){
	        	paramsMap.put(CompressHandler.Params.BUCKET_NAME, bucketname);
	        }else if ("2".equals(type)){
	        	paramsMap.put(CompressHandler.Params.BUCKET_NAME, bucketname1);
	        }
	        
	        //回调地址
	        paramsMap.put(CompressHandler.Params.NOTIFY_URL, "http://sp.bangkanche.com/zipFinish");
	        //选择处理任务
	        paramsMap.put(CompressHandler.Params.APP_NAME, "compress");

	        //已json格式生成任务信息
	        JSONArray array = new JSONArray();
	        JSONObject json = new JSONObject();

	        JSONArray array2 = new JSONArray();
	        if(null==imgs||imgs.length==0){
	        	return null;
	        }else{
	        	for(int i=0;i<imgs.length;i++){
	        		array2.add(imgs[i]);
	        		//  array2.put("/testZipIn/Tulips.jpg");
	        	}
	        }
	      
	        //添加处理参数
	        json.put(CompressHandler.Params.SOURCES, array2);
	        json.put(CompressHandler.Params.SAVE_AS, "/tempZipOut/"+fileName);
	        array.add(json);

	        //添加任务信息
	        paramsMap.put(CompressHandler.Params.TASKS, array);

	        try {
	            Result result = handler.process(paramsMap);
	            if (result.isSucceed()) {
	                String[] ids = handler.getTaskId(result.getMsg());
	                return Arrays.toString(ids);
	            }else{
	            	return null;
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null;
	        }
	}
	
}
